package com.kim.store.contorller;

import com.kim.store.service.ICartService;
import com.kim.store.util.JsonResult;
import com.kim.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController extends BaseController{
    @Autowired
    private ICartService cartService;

    @RequestMapping("/add_to_cart")
    public JsonResult<Void> addToCart(HttpSession session,Integer pid,Integer amount){
        cartService.addToCart(getUidFromSession(session),pid,
                         amount,getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }

    @RequestMapping({"/",""})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session){
        List<CartVO> data = cartService.getVOByUid(getUidFromSession(session));
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("/num/add/{cid}")
    public JsonResult<Integer> addNum(HttpSession session,
                                          @PathVariable Integer cid){
        Integer num = cartService.addNum(cid,
                   getUidFromSession(session),
                   getUsernameFromSession(session));
        return new JsonResult<>(OK,num);
    }

    @RequestMapping("/num/reduce/{cid}")
    public JsonResult<Integer> reduceNum(HttpSession session,
                                      @PathVariable Integer cid){
        Integer num = cartService.reduceNum(cid,
                getUidFromSession(session),
                getUsernameFromSession(session));
        return new JsonResult<>(OK,num);
    }

    @RequestMapping("/list")
    public JsonResult<List<CartVO>> getVOByCid(HttpSession session,Integer[] cids){
        List<CartVO> list = cartService.getVOByCid(cids, getUidFromSession(session));
        return new JsonResult<>(OK,list);
    }
}
